package com.byd.sealmanage.util;


import org.apache.commons.lang3.StringUtils;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * String Util
 *
 * @author lichao
 * @version V1.0
 * @since 2015-1-6
 */
public class StringUtil {
    private static final Pattern p = Pattern.compile("\\s*|\t|\r|\n");

    /**
     * 检查指定的字符串是否为空。
     * <ul>
     * <li>SysUtils.isEmpty(null) = true</li>
     * <li>SysUtils.isEmpty("") = true</li>
     * <li>SysUtils.isEmpty("   ") = true</li>
     * <li>SysUtils.isEmpty("abc") = false</li>
     * </ul>
     *
     * @param value
     *            待检查的字符串
     * @return true/false
     */
    public static boolean isEmpty(String value) {
        int strLen;
        if (value == null || (strLen = value.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(value.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断字符串是否为空
     *
     * @param str null、""、"null"都返回true
     */
    public static boolean isNullString(String str) {
        return null == str || StringUtils.isBlank(str.trim()) || "null".equals(str.trim().toLowerCase());
    }

    /**
     * 格式化字符串 如果为空，返回""
     */
    public static String formatString(String str) {
        if (isNullString(str)) {
            return "";
        } else {
            return str;
        }
    }

    /**
     * 判断是否是整数类型
     */
    public static boolean isInteger(String value) {
        if (value == null) {
            return false;
        }
        try {
            Integer.parseInt(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean isLong(String value) {
        if (value == null) {
            return false;
        }
        try {
            Long.parseLong(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    public static boolean isShort(String value) {
        if (value == null) {
            return false;
        }
        try {
            Short.parseShort(value);
            return true;
        } catch (NumberFormatException e) {
            return false;
        }
    }

    /**
     * 获取中文字符串每个汉字的首字母
     */
    public static String convertZH(String getstring) {

        String backgetchar = "";
        if (StringUtil.isChinese(getstring)) {
            String[] str = getstring.split("");
            int length = str.length;
            int start = 1;
            byte[] a;
            String getchar = "";
            while (start < length) {
                a = str[start].getBytes();
                if (a.length > 1) {
                    int asc = 256 * (a[0] + 256) + (a[1] + 256);
                    if (asc >= 45217 && asc <= 45252) {
                        getchar = "A";
                    } else if (asc >= 45253 && asc <= 45760) {
                        getchar = "B";
                    } else if (asc >= 45761 && asc <= 46317) {
                        getchar = "C";
                    } else if (asc >= 46318 && asc <= 46825) {
                        getchar = "D";
                    } else if (asc >= 46826 && asc <= 47009) {
                        getchar = "E";
                    } else if (asc >= 47010 && asc <= 47296) {
                        getchar = "F";
                    } else if (asc >= 47297 && asc <= 47613) {
                        getchar = "G";
                    } else if (asc >= 47614 && asc <= 48118) {
                        getchar = "H";
                    } else if (asc >= 48119 && asc <= 49061) {
                        getchar = "J";
                    } else if (asc >= 49062 && asc <= 49323) {
                        getchar = "K";
                    } else if (asc >= 49324 && asc <= 49895) {
                        getchar = "L";
                    } else if (asc >= 49896 && asc <= 50370) {
                        getchar = "M";
                    } else if (asc >= 50371 && asc <= 50613) {
                        getchar = "N";
                    } else if (asc >= 50614 && asc <= 50621) {
                        getchar = "O";
                    } else if (asc >= 50622 && asc <= 50905) {
                        getchar = "P";
                    } else if (asc >= 50906 && asc <= 51386) {
                        getchar = "Q";
                    } else if (asc >= 51387 && asc <= 51445) {
                        getchar = "R";
                    } else if (asc >= 51446 && asc <= 52217) {
                        getchar = "S";
                    } else if (asc >= 52218 && asc <= 52967) {
                        getchar = "T";
                    } else if (asc >= 52698 && asc <= 52979) {
                        getchar = "W";
                    } else if (asc >= 52980 && asc <= 53640) {
                        getchar = "X";
                    } else if (asc >= 53689 && asc <= 54480) {
                        getchar = "Y";
                    } else if (asc >= 54481 && asc <= 62289) {
                        getchar = "Z";
                    }
                } else {
                    getchar = str[start];
                }
                backgetchar += getchar;
                start++;
            }
        } else {
            String[] initc = getstring.split(" ");
            for (int i = 0; i < initc.length; i++) {
                backgetchar += initc[i].substring(0, 1);
            }
        }

        return backgetchar;
    }

    public static boolean isChinese(String str) {
        boolean flag = false;
        for (int i = 0; i < str.length(); i++) {
            char ch = str.charAt(i);
            if (!(ch > 0x7e || ch < 0x21)) {
                flag = true;
                break;
            }
        }
        return flag;
    }

    /**
     * 判断字符串是否为数字
     *
     * @param stringInput 要进行判断的字符串。
     * @return 如果非空返回True，否则返回False。
     */
    public static boolean isDigit(String stringInput) {
        if (stringInput == null) {
            return false;
        }
        if ("".endsWith(stringInput)) {
            return false;
        }
        char[] times = stringInput.trim().toCharArray();
        for (char ch : times) {
            if (!Character.isDigit(ch)) {
                return false;
            }
        }
        return true;
    }

    /**
     * 判断字符串是否为数字
     *
     * @param stringInput 要进行判断的字符串。
     * @return 如果非空返回True，否则返回False。
     */
    public static boolean isDigit(int stringInput) {
        return stringInput >= 0;
    }

    public static boolean isChineseRex(String str) {

        boolean flag = false;
        if (str == null || "".equals(str)) {
            flag = true;
        }
        assert str != null;
        String rexStr = str.replaceAll("[^\\u4e00-\\u9fa5]", "");
        if (rexStr.length() != str.length()) {
            flag = true;
        }
        return flag;
    }

    /**
     * 将指定的数字转化为指定长度的字符串，多余部分用"#"填充。例如：intToStrWithSharp(1000, 6)->"##1000"
     *
     * @param value  要转换的整数
     * @param length 转换后的字符串长度
     * @return 转换后的字符串，如果指定的长度小于整数的位数，则只返回数字。例如：intToStrWithSharp(1000,
     * 2)->"1000"
     */
    public static String intToStrWithSharp(Integer value, int length, char c) {
        int valueLength = value.toString().length();
        int diff = length - valueLength;

        if (value < Integer.MAX_VALUE) {
            StringBuilder sb = new StringBuilder();
            for (int i = 0; i < diff; i++) {
                sb.append(c);
            }
            sb.append(value.intValue());
            return sb.toString();
        } else {
            return "-1";
        }
    }

    /**
     * 除字符串中的空格、回车、换行符、制表符
     * @param str 需要处理的字符串
     * @return 处理后的字符串
     */
    public static String replaceBlank(String str) {
        String dest = "";
        if (str != null) {
            Matcher m = p.matcher(str);
            dest = m.replaceAll("");
        }
        return dest;
    }

    /**
     * 除字符串中 ASCLL为160的空格,该空格不能通过replace \s 以及trim的方法去除。
     *
     * @param str 需要处理的字符串
     * @return 处理后的字符串
     */
    public static String replaceSpecialBlank(String str) {
        String dest = str.replaceAll("[\\s\\u00A0]+$", "");
        return dest;
    }
}
